﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EnhancedWebControls
{
    [DefaultProperty("Checked"), 
    ToolboxData("<{0}:EnhancedCheckBox runat=server></{0}:EnhancedCheckBox>"),
    DefaultEvent("CheckedChanged")]
    public class EnhancedCheckBox : LabeledInput
    {
        #region Events
        public event EventHandler CheckedChanged;
        private void RightCheckBox_CheckedChanged(object sender, System.EventArgs e)
        {
            OnCheckChanged(this, e);
        }
        protected virtual void OnCheckChanged(object sender, System.EventArgs e)
        {
            if (CheckedChanged != null)
                CheckedChanged(this, e);
        }
        #endregion

        #region Child Controls
        private CheckBox _rightCheckBox;
        private Label _descriptionLabel;
        #endregion
        
        #region Properties
        public bool DisableCheckBoxPixelShift {get;set;}
        [Localizable(true)]
        public string FieldDescription
        {
            get 
            {
                EnsureChildControls();
                return _descriptionLabel.Text;
            }
            set 
            {
                EnsureChildControls();
                _descriptionLabel.Text = value;
            }
        }
        public bool Checked
        {
            get 
            {
                EnsureChildControls();
                return _rightCheckBox.Checked;
            }
            set 
            {
                EnsureChildControls();
                _rightCheckBox.Checked = value;
                //_rightLabel.Text = Checked.ToString();
            }
        }
        public bool AutoPostBack
        {
            get 
            {
                EnsureChildControls();
                return _rightCheckBox.AutoPostBack;
            }
            set 
            {
                EnsureChildControls();
                _rightCheckBox.AutoPostBack = value;
            }
        }
        public CheckBox RightCheckbox
        {
            get 
            {
                EnsureChildControls();
                return _rightCheckBox;
            }
        }
        public override string ToolTip
        {
            get 
            {
                EnsureChildControls();
                return base.ToolTip;
            }
            set 
            {
                EnsureChildControls();            
                base.ToolTip = value;
                _rightCheckBox.ToolTip = value;
                _descriptionLabel.ToolTip = value;
            }
        }
        #endregion

        #region Overriden Methods
        public EnhancedCheckBox()
            : base()
        {

        }
        protected override void CreateChildControls()
        {
            Controls.Clear();
            base.CreateChildControls();

            _rightCheckBox = new CheckBox();
            _rightCheckBox.ID = "RightCheckBox";
            _rightCheckBox.Text = string.Empty;
            _rightCheckBox.CheckedChanged += new EventHandler(RightCheckBox_CheckedChanged);
            Controls.Add(_rightCheckBox);

            _descriptionLabel = new Label();
            _rightCheckBox.ID = "DescLabel";
            _rightCheckBox.Text = string.Empty;
            Controls.Add(_descriptionLabel);
            _leftLabel.AssociatedControlID = _rightCheckBox.ID;
        }
        protected override void OnPreRender(System.EventArgs e)
        {
            base.OnPreRender(e);
            if (Visible && !LabelMode && Enabled)
                SetCheckBoxClientScripts();
        }
        protected override void Render(HtmlTextWriter writer)
        {
            EnsureChildControls();
            RenderDesignTimeStyles(writer);

            if (!DesignMode)
            {
                if (!DisableCheckBoxPixelShift && (this.Page.Request.Browser.IsBrowserIE()))
                {
                    if (!LabelMode)
                    {
                        if (LabelWidth.Value > 0 && LabelWidth.Type == UnitType.Pixel)
                            LabelWidth = Unit.Pixel((int)LabelWidth.Value - 5);
                        if (FieldWidth.Value > 0 && FieldWidth.Type == UnitType.Pixel)
                            FieldWidth = Unit.Pixel((int)FieldWidth.Value + 5);
                    }
                }
            }

            RenderContainerBegin(writer, true);
            RenderLeftSide(writer);
            RenderRightContainerBegin(writer);
            if (LabelMode)
            {
                _rightLabel.CssClass = "EWC__Label";
                _rightLabel.Text = Checked.ToString();
                _rightLabel.RenderControl(writer);
                _rightCheckBox.Style["display"] = "none";
                _descriptionLabel.Style["display"] = "none";
                _rightCheckBox.RenderControl(writer);
                _descriptionLabel.RenderControl(writer);
            }
            else
            {
                _rightCheckBox.Style["overflow"] = "hidden";
                _rightCheckBox.Style["float"] = "left";
                _rightCheckBox.Style["margin-top"] = "2px";
                _rightCheckBox.Style["margin-right"] = "0px";
                _rightCheckBox.Style["margin-bottom"] = "0px";
                _rightCheckBox.Style["margin-left"] = "1px";
                _rightCheckBox.Style["border"] = "none";
                _rightCheckBox.Style["padding"] = "0px";
                _descriptionLabel.CssClass = "EWC__DescriptionLabel";
                _descriptionLabel.Style["display"] = "block";
                _descriptionLabel.Style["float"] = "left";
                _descriptionLabel.Style["padding-left"] = "4px";
                if (FieldWidth.Value > 0 && FieldWidth.Type == UnitType.Pixel)
                    _descriptionLabel.Style["width"] = Unit.Pixel((int)FieldWidth.Value - 26).ToString();
                RenderCheckBoxOuterDivBegin(writer);
                _rightCheckBox.RenderControl(writer);
                RenderCheckBoxOuterDivEnd(writer);
                _descriptionLabel.RenderControl(writer);
            }
            RenderRightContainerEnd(writer);
            RenderContainerEnd(writer);
        }
        #endregion

        #region Helper Method
        protected void SetCheckBoxClientScripts()
        {
            string labelClass = "EWC__Label";
            if (ErrorMode)
                labelClass = "EWC__ErrorLabel";
            _rightCheckBox.InputAttributes.Add("onblur", "document.getElementById('" + _leftLabel.ClientID + "').className= '" + labelClass + "';");
            _rightCheckBox.InputAttributes.Add("onfocus", "document.getElementById('" + _leftLabel.ClientID + "').className= 'EWC__ActiveLabel';");
        }
        protected void RenderCheckBoxOuterDivBegin(HtmlTextWriter writer)
        {
            writer.Write("<div style='border:none;width:22px;height:22px;overflow:hidden;float:left;clear:none;'>");
        }
        protected void RenderCheckBoxOuterDivEnd(HtmlTextWriter writer)
        {
            writer.Write("</div>");
        }
        #endregion
    }
}
